/**   
 * 项目名：化工企业安全生产信息化管理平台
 * 日期：    2020-04-18 23:54:04  
 * Copyright (c) 2015- -版权所有     
 */
package com.joysuch.wwyt.core.controller;

import javax.validation.Valid;

import cn.zlg.common.springmvc.validate.constraints.NotNull;

import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.bean.SimpleSelectListBean;
import com.joysuch.wwyt.core.service.SimpleSelectListService;
import com.joysuch.wwyt.core.bean.BaseRoleEditBean;
import com.joysuch.wwyt.core.bean.BaseRoleListBean;
import static com.joysuch.wwyt.core.bean.BaseRoleListBean.SearchBaseRole;
import com.joysuch.wwyt.core.entity.BaseRole;
import com.joysuch.wwyt.core.service.BaseRoleService;
import java.util.List;

import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "系统管理-角色管理")
@RequestMapping("/base/role")
public class BaseRoleController {

    private static final Logger logger = LoggerFactory.getLogger(BaseRoleController.class);

    @Autowired
    private BaseRoleService baseRoleService;
    @Autowired
    private SimpleSelectListService simpleSelectListService;

    @RequiresPermissions("base:role:add")
    @RequestMapping("add")
    public Object add(@Valid @RequestBody BaseRoleEditBean entity) {
        BaseRole add = baseRoleService.add(entity);
        return ResultBean.success(add);
    }

    @RequiresPermissions("base:role:update")
    @RequestMapping("update")
    public Object update(@Valid @RequestBody BaseRoleEditBean entity) {
        if (entity.getId() == null) {
            return ResultBean.fail(102, "未指定主键");
        }
        baseRoleService.update(entity);
        return ResultBean.success("");
    }

    @RequiresPermissions("base:role")
    @RequestMapping("page")
    public Object page(Pageable page, SearchBaseRole condition) {
        Page<BaseRoleListBean> data = baseRoleService.findByPage(page,condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequiresPermissions("base:role")
    @RequestMapping("select-page")
    public Object selectPage(Pageable page, SearchBaseRole condition) {
        Page<BaseRoleListBean> data = baseRoleService.findByPage(page,condition);
        return ResultBean.pageData(data.getContent(), data.getTotalElements());
    }

    @RequestMapping("simple-list")
    public Object simpleList() {
        List<SimpleSelectListBean> data = simpleSelectListService.list(BaseRole.class, "name");
        return ResultBean.success(data);
    }

    @RequiresPermissions("base:role:delete")
    @RequestMapping("delete")
    public Object delete(Long id) throws Exception {
        baseRoleService.delete(id);
        return ResultBean.success("");
    }

    @RequiresPermissions("base:role")
    @RequestMapping("get")
    public Object getById(@Valid @NotNull(field="id") Long id) {
        return ResultBean.success(baseRoleService.getById(id));
    }
}

